using FontStyle = System.Drawing.FontStyle;
using MessageBox = System.Windows.Forms.MessageBox;
using Point = System.Drawing.Point;
using Size = System.Drawing.Size;

namespace FileTransferDesktop.Views.Forms;

public partial class WinFormsExampleControl : System.Windows.Forms.UserControl
{
    private System.Windows.Forms.Button sampleButton;
    private System.Windows.Forms.TextBox sampleTextBox;
    private System.Windows.Forms.Label sampleLabel;
    private System.Windows.Forms.ProgressBar sampleProgressBar;
    private System.Windows.Forms.ListBox sampleListBox;
    private System.Windows.Forms.CheckBox sampleCheckBox;
    private System.Windows.Forms.DateTimePicker sampleDatePicker;

    public WinFormsExampleControl()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        this.SuspendLayout();

        // Set control properties
        this.Size = new Size(400, 300);
        this.BackColor = System.Drawing.Color.White;
        this.BorderStyle = BorderStyle.FixedSingle;

        // Sample Label
        this.sampleLabel = new System.Windows.Forms.Label
        {
            Text = "WinForms 示例控件",
            Location = new Point(20, 20),
            Size = new Size(200, 25),
            Font = new Font("Microsoft YaHei", 12, FontStyle.Bold),
            ForeColor = System.Drawing.Color.DarkBlue,
        };

        // Sample TextBox
        this.sampleTextBox = new System.Windows.Forms.TextBox
        {
            Location = new Point(20, 55),
            Size = new Size(200, 25),
            Text = "输入一些文本...",
            Font = new Font("Microsoft YaHei", 9),
        };

        // Sample Button
        this.sampleButton = new System.Windows.Forms.Button
        {
            Text = "点击我",
            Location = new Point(230, 55),
            Size = new Size(100, 25),
            BackColor = System.Drawing.Color.LightBlue,
            Font = new Font("Microsoft YaHei", 9),
            UseVisualStyleBackColor = false,
        };
        this.sampleButton.Click += SampleButton_Click;

        // Sample CheckBox
        this.sampleCheckBox = new System.Windows.Forms.CheckBox
        {
            Text = "启用功能",
            Location = new Point(20, 90),
            Size = new Size(100, 25),
            Font = new Font("Microsoft YaHei", 9),
        };
        this.sampleCheckBox.CheckedChanged += SampleCheckBox_CheckedChanged;

        // Sample DateTimePicker
        this.sampleDatePicker = new System.Windows.Forms.DateTimePicker
        {
            Location = new Point(20, 125),
            Size = new Size(200, 25),
            Font = new Font("Microsoft YaHei", 9),
        };

        // Sample ProgressBar
        this.sampleProgressBar = new System.Windows.Forms.ProgressBar
        {
            Location = new Point(20, 160),
            Size = new Size(200, 20),
            Value = 50,
            Style = ProgressBarStyle.Continuous,
        };

        // Sample ListBox
        this.sampleListBox = new System.Windows.Forms.ListBox
        {
            Location = new Point(20, 190),
            Size = new Size(200, 80),
            Font = new Font("Microsoft YaHei", 9),
        };
        this.sampleListBox.Items.AddRange(
            new object[] { "项目 1", "项目 2", "项目 3", "项目 4", "项目 5" }
        );

        // Add controls to the UserControl
        this.Controls.Add(this.sampleLabel);
        this.Controls.Add(this.sampleTextBox);
        this.Controls.Add(this.sampleButton);
        this.Controls.Add(this.sampleCheckBox);
        this.Controls.Add(this.sampleDatePicker);
        this.Controls.Add(this.sampleProgressBar);
        this.Controls.Add(this.sampleListBox);

        this.ResumeLayout(false);
    }

    private void SampleButton_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrWhiteSpace(sampleTextBox.Text))
        {
            MessageBox.Show(
                $"你输入的文本是: {sampleTextBox.Text}",
                "WinForms 消息",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information
            );

            // Update progress bar
            sampleProgressBar.Value = (sampleProgressBar.Value + 10) % 101;

            // Add item to listbox
            sampleListBox.Items.Add($"新项目 - {DateTime.Now:HH:mm:ss}");
            sampleListBox.SelectedIndex = sampleListBox.Items.Count - 1;
        }
    }

    private void SampleCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        bool isEnabled = sampleCheckBox.Checked;
        sampleButton.Enabled = isEnabled;
        sampleTextBox.Enabled = isEnabled;
        sampleDatePicker.Enabled = isEnabled;

        sampleLabel.Text = isEnabled ? "WinForms 示例控件 (已启用)" : "WinForms 示例控件 (已禁用)";
        //sampleLabel.ForeColor = isEnabled ? Color.DarkGreen : Color.Gray;
    }

    // Public method to demonstrate interaction from WPF
    public void UpdateFromWPF(string message)
    {
        sampleTextBox.Text = message;
        sampleProgressBar.Value = new Random().Next(0, 101);
    }

    // Event to notify WPF about changes
    public event EventHandler<string> DataChanged;

    protected virtual void OnDataChanged(string data)
    {
        DataChanged?.Invoke(this, data);
    }
}
